package com.mbuswidget;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MBusWidgetConfigurationStopsAddStop extends Activity {
	private int widgetID = AppWidgetManager.INVALID_APPWIDGET_ID;
	private ListView itemList;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.selectstopby);
		setResult(RESULT_CANCELED);

		TextView topLabel = (TextView) findViewById(R.id.Toplbl);
		topLabel.setText(R.string.chooseStopSelectionMethodTxt);

		widgetID = getIntent().getIntExtra(
				AppWidgetManager.EXTRA_APPWIDGET_ID,
				AppWidgetManager.INVALID_APPWIDGET_ID);
		
		if(widgetID == AppWidgetManager.INVALID_APPWIDGET_ID) {
			finish();
		}

		String[] methods = {
				getResources().getString(
						R.string.chooseStopSelectionMethodByStopTxt),
				getResources().getString(
						R.string.chooseStopSelectionMethodByRouteTxt) };
		itemList = (ListView) findViewById(R.id.itemList);
		itemList.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, methods));
		itemList.setOnItemClickListener(new OnItemClickListener() {
			// @Override
			public void onItemClick(AdapterView<?> a, View v, int position,
					long id) {
//				Toast.makeText(MBusWidgetConfigurationStopsAddStop.this,
//						"You clicked " + position + "," + id,
//						Toast.LENGTH_SHORT);
				if (id == 0) {
					// This is show all stops
					Intent intent = new Intent(
							MBusWidgetConfigurationStopsAddStop.this,
							MBusWidgetConfigurationStopsAddStopByAllStops.class);
					intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
							widgetID);
					startActivityForResult(intent, 0);
				} else if (id == 1) {
					// This is stops by route
					Intent intent = new Intent(
							MBusWidgetConfigurationStopsAddStop.this,
							MBusWidgetConfigurationStopsAddStopByRoute.class);
					intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
							widgetID);
					startActivityForResult(intent, 0);
					
				}
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case (0): {
			if (resultCode == Activity.RESULT_OK) {
				// We have information required. Complete Widget setup.

				String[] stopList = data.getExtras().getStringArray(
						MBusWidgetConstants.INTENT_SELECTED_STOPS);

				Intent intent = getIntent();
				intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
						widgetID);
				// intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
				// widgetID);
				intent.putExtra(MBusWidgetConstants.INTENT_SELECTED_STOPS,
						stopList);
				intent.putExtra(MBusWidgetConstants.STOP_TYPE, MBusWidgetConstants.ADD_STOP);

				setResult(RESULT_OK, intent);
				finish();
			}
			break;
		}
		}
	}
}
